/**
 * 
 */
package org.sandholm.max.MCFriendsList;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class MCFriendsInfoActivity extends Activity {
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.info);
        Bundle extras = getIntent().getExtras();
		unameStr = extras.getString("uname");
		unameLabel = (TextView) this.findViewById(R.id.textView2);
		unameLabel.setText(unameStr);
		try {
			i = (ImageView) findViewById(R.id.imageView1);
			skin = BitmapFactory.decodeStream((InputStream)new URL("http://www.minecraft.net/skin/"+unameStr+".png").getContent()); 
		} catch (FileNotFoundException e) {
			try {
				skin = BitmapFactory.decodeStream((InputStream)new URL("http://www.minecraft.net/skin/char.png").getContent());
			} catch (MalformedURLException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
			face = Bitmap.createBitmap(skin, 8, 8, 8, 8);
			face = Bitmap.createScaledBitmap(face, 64, 64, false);
			i.setImageBitmap(face);
        
		try {
			haspaidStr = inputStreamAsString((InputStream) new URL("http://www.minecraft.net/haspaid.jsp?user="+unameStr).getContent());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		haspaidLabel = (TextView) findViewById(R.id.textView1);
		if (haspaidStr.contentEquals("true")) {
			haspaidLabel.setText("Has bought Minecraft");
		}
		else {
			haspaidLabel.setText("Hasn't bought Minecraft");
		}
    }
	
	public static String inputStreamAsString(InputStream stream) throws IOException {
		BufferedReader BR = new BufferedReader(new InputStreamReader(stream));
		StringBuilder SB = new StringBuilder();
		String line1 = null;

		while ((line1 = BR.readLine()) != null) {
			SB.append(line1);
		}

		BR.close();
		return SB.toString();
	}
	
	private String unameStr;
	private TextView unameLabel;
	private String haspaidStr;
	private TextView haspaidLabel;
	private ImageView i;
	private Bitmap skin;
	private Bitmap face;
}
